package org.wdcode.base.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.wdcode.common.constants.StringConstants;
import org.wdcode.common.params.WdParams;

/**
 * 初始化监听器 在web.xml中配置 configLocation 配置文件位置,参数文件默认在classPath下
 * <p>
 * <listener> <listener-class>
 * org.springframework.web.context.ContextLoaderListener </listener-class>
 * </listener> <context-param> <param-name>config</param-name>
 * <param-value>../config/config.xml</param-value> </context-param>
 * <p>
 * @author WD
 * @since JDK6
 * @version 1.0 2009-09-08
 */
public class WdInitListener implements ServletContextListener {
	/**
	 * 初始化资源
	 */
	public void contextInitialized(ServletContextEvent event) {
		// 工程路径Key
		String path = "path";
		// 获得Servlet上下文
		ServletContext context = event.getServletContext();
		// 设置工程路径为path
		context.setAttribute(path, context.getContextPath());
		// 配置系统路径
		System.setProperty(path, context.getRealPath(StringConstants.EMPTY));
		// 获得配置
		WdParams.setConfig(context.getInitParameter("wdConfigLocation"));
	}

	/**
	 * 销毁资源
	 */
	public void contextDestroyed(ServletContextEvent event) {}
}
